package com.dk.base.core.service;

import com.dk.base.common.model.DemoDTO;
import com.dk.base.common.model.DemoRelDTO;
import com.dk.base.core.dbbase.*;
import com.dk.common.distribute.DistObjectFactory;
import com.dk.common.distribute.map.DistMap;
import com.dk.common.result.ApiPageResult;
import com.dk.shared.events.demo.DemoEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DemoService {

    private static final QDemoRelPO qDemoRelPO = QDemoRelPO.demoRelPO;

    @Autowired
    private ApplicationEventPublisher eventPublisher;


    @Autowired
    private DemoRelRepository demoRelRepository;
    @Autowired
    private DemoRepository demoRepository;


    @Autowired
    protected DistObjectFactory distObjectFactory;


    public List<DemoDTO> list() {
        DistMap<String, List> map = distObjectFactory.getCache("abc");
        List list = map.get("list");
        if (list == null) {
            list = demoRepository.findAll();
            map.fastPutIfAbsent("list", list);
        }

        return demoRepository.findAll().stream().map(po ->
                        DemoDTO.builder()
                                .name(po.getName())
                                .age(po.getAge())
                                .id(po.getId()).build())
                .toList();
    }


    public Integer insert(Integer num) {

        eventPublisher.publishEvent(DemoEvent.of(num));


        return num;
    }

    public void saveByEvent(Integer num) {
        int batch = (num - 1) / 1000 + 1;
        for (int index = 0; index < batch; index++) {
            List<DemoPO> list = new ArrayList();
            int size = index == batch - 1 ? (num - index * 1000) : 1000;
            for (int i = 0; i < size; i++) {
                DemoPO demoPO = new DemoPO();
                demoPO.setName(i + "2");
                demoPO.setAge(i);
                list.add(demoPO);
            }
            demoRepository.saveAll(list);
            List<DemoRelPO> list2 = new ArrayList();
            for (int i = 0; i < size; i++) {
                DemoRelPO demoRelPO = new DemoRelPO();
                demoRelPO.setType("A");
                demoRelPO.setDatasetId(1L);
                demoRelPO.setDemo(list.get(i));
                list2.add(demoRelPO);
            }

            demoRelRepository.saveAll(list2);
        }
    }

    public List<DemoRelDTO> query(Long demoId) {
        List<DemoRelPO> list = (List<DemoRelPO>) demoRelRepository.findAll(qDemoRelPO.demo.id.eq(demoId));
        return list.stream().map(this::convertDTO).toList();
    }


    public ApiPageResult<DemoRelDTO> page(Long datasetId) {
        return ApiPageResult.with(demoRelRepository.findAll(qDemoRelPO.datasetId.eq(datasetId)
                , QPageRequest.of(0, 10, qDemoRelPO.createdDate.desc()))
                        .map(this::convertDTO));
    }

    private DemoRelDTO convertDTO(DemoRelPO po) {
        return DemoRelDTO.builder()
                .id(po.getId())
                .type(po.getType())
                .datasetId(po.getDatasetId())
                .demo(DemoDTO.builder()
                        .id(po.getDemo().getId())
                        .age(po.getDemo().getAge())
                        .name(po.getDemo().getName())
                        .build())
                .build();
    }
}
